﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class MasProvinceDAL : IMasProvinceDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public MasProvinceDAL()
        {

        }
        
        public IList<MasProvinceInfo> ListMasProvinces(MasProvinceInfo proInfo)
        {
            string strSQL;
            int regSeq = 0;

            if (proInfo.Regions != null)
                regSeq = proInfo.Regions.Sequence;

            List<MasProvinceInfo> infoList = new List<MasProvinceInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@Seq", proInfo.Sequence);
                dbManager.AddParameters(2, "@Region", regSeq);

                strSQL = "SELECT p.PROV_SEQ, p.PROV_NAME, p.PROV_NAME_ENG"
                        + ", p.CREATED_BY, p.UPDATED_BY, p.REGION_SEQ"
                    + " FROM MAS_PROVINCES p"
                    + " WHERE p.RECORD_STATUS not like @DeleteRecord"
                    + (proInfo.Sequence > 0 ? " AND p.PROV_SEQ = @Seq" : "")
                    + (regSeq > 0 ? " AND p.REGION_SEQ = @Region" : "")
                    + " ORDER BY p.PROV_NAME";

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {    
                    MasRegionDAL regionDAL = new MasRegionDAL();
                    MasRegionInfo regionInfo = new MasRegionInfo();
                    IList<MasRegionInfo> regionList = regionDAL.ListMasRegions(new MasRegionInfo(rdr.IsDBNull(5) ? 0 : rdr.GetInt32(5)));
                    if (regionList.Count > 0)
                        regionInfo = regionList[0];

                    MasProvinceInfo Info = new MasProvinceInfo(rdr.IsDBNull(0) ? null : (Nullable<int>)rdr.GetInt32(0)
                            , rdr.IsDBNull(1) ? "" : rdr.GetString(1)
                            , rdr.IsDBNull(2) ? "" : rdr.GetString(2)
                            , regionInfo
                            , rdr.IsDBNull(3) ? null : (Nullable<int>)rdr.GetInt32(3)
                            , rdr.IsDBNull(4) ? null : (Nullable<int>)rdr.GetInt32(4));
                    infoList.Add(Info);
                }

                return infoList;
            }
            catch
            {
                return null;
                throw;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }
        public DataSet LoadMasProvinces(Nullable<int> Sequence, Nullable<int> Region)
        {
            string strSQL;
            DataSet ds = null;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@ActiveRecord", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(1, "@Sequence", Sequence);
                dbManager.AddParameters(2, "@Region", Region);

                strSQL = "SELECT p.PROV_SEQ, p.PROV_NAME, p.PROV_NAME_ENG"
                        + ", p.CREATED_BY, p.UPDATED_BY, p.REGION_SEQ"
                    + " FROM MAS_PROVINCES p"
                    + " WHERE p.RECORD_STATUS = @ActiveRecord"
                    + (Sequence.HasValue ? " AND p.PROV_SEQ = @Sequence" : "")
                    + (Region.HasValue ? " AND p.REGION_SEQ = @Region" : "")
                    + " ORDER BY p.PROV_NAME";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }

            return ds;
        }
        public void Insert(MasProvinceInfo info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@Name", info.Name);
                dbManager.AddParameters(1, "@NameEng", info.NameEng);
                dbManager.AddParameters(2, "@Region", info.Regions.Sequence);
                dbManager.AddParameters(3, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(4, "@CreatedBy", info.CreatedBy);
                dbManager.AddParameters(5, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO MAS_PROVINCES(PROV_NAME, PROV_NAME_ENG, REGION_SEQ"
                        + ", RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES(@Name, @NameEng, @Region, @RecordStatus, @CreatedBy, @CreatedWhen)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(MasProvinceInfo info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@Seq", info.Sequence);
                dbManager.AddParameters(1, "@Name", info.Name);
                dbManager.AddParameters(2, "@NameEng", info.NameEng);
                dbManager.AddParameters(3, "@Region", info.Regions.Sequence);
                dbManager.AddParameters(4, "@UpdatedBy", info.UpdatedBy);
                dbManager.AddParameters(5, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE MAS_PROVINCES"
                           + " SET PROV_NAME = @Name"
                                + ", PROV_NAME_ENG = @NameEng"
                                + ", REGION_SEQ = @Region"
                                + ", UPDATED_BY = @UpdatedBy"
                                + ", UPDATED_WHEN = @UpdatedWhen"
                           + " WHERE PROV_SEQ = @Seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(int Sequence, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@seq",  Sequence);
                dbManager.AddParameters(1, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@updated_by", UpdatedBy);
                dbManager.AddParameters(3, "@updated_when", DateTime.Today);

                strSQL = "UPDATE MAS_PROVINCES"
                    + " SET RECORD_STATUS = @record_status"
                        + ", UPDATED_BY = @updated_by"
                        + ", UPDATED_WHEN = @updated_when"
                    + " WHERE PROV_SEQ = @seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
